<template>
  <div>
    <div>
       <el-form :inline="true" :model="jl" class="user-search">
      <el-form-item label="搜索：">
         <el-input size="small" v-model="jl.code" placeholder="职位编码" 
                style="width: 300px"></el-input>
      </el-form-item>
     

      <el-input size="small" v-model="jl.positionName" placeholder="职位名称" 
      style="width: 300px"></el-input>

      <!-- <el-select v-model="jl.positionName" placeholder="职位级别名称" style="margin-left: 8px" size="small">
        <el-option
            v-for="item in titleLevels"
            :key="item"
            :label="item"
            :value="item">
        </el-option>
      </el-select> -->
    
        <el-button size="small" type="primary" icon="el-icon-search" @click="search">搜索</el-button>
        <el-button size="small" type="primary" icon="el-icon-edit" @click="flush">重置</el-button>
       </el-form>
      <el-button size="small" style="margin-left: 8px" icon="el-icon-plus" type="primary" @click="addJobLevel">添加</el-button>
    </div>

    <div style="margin-top: 10px" >
       <el-table
       :data="jls"
          stripe
          border
         @selection-change="handleSelectionChange"
    style="width: 100%"
    max-height="250">
     <el-table-column 
     fixed
     type="selection"
     width="55">
    </el-table-column>
     <el-table-column
            prop="code"
            label="职位编码"
            width="120">
        </el-table-column>
        <el-table-column
            prop="categoryCode"
            label="职位类别编码"
            width="150">
        </el-table-column>
        <el-table-column
            prop="positionName"
            label="职位名称"
            width="150">
        </el-table-column>
        <el-table-column
            prop="leveCode"
            label="职位级别"
            width="150">
        </el-table-column>
        <el-table-column
            prop="categoryName"
            label="职位类别名称"
            width="150">
        </el-table-column>
        <el-table-column
            prop="enabled"
            label="是否可用"
            width="150">
          <template slot-scope="scope">
            <el-tag type="success" v-if="scope.row.enabled">已启用</el-tag>
            <el-tag type="danger" v-else >未启用</el-tag>
          </template>
        </el-table-column>
        <el-table-column
            prop="remark"
            label="职位描述"
            width="150">
        </el-table-column>

    <el-table-column
      fixed="right"
      label="操作"
      width="150">
       <template slot-scope="scope">
            <el-button size="small" @click="showEditDialog(scope.row)">编辑</el-button>
            <el-button size="small" type="danger" @click="deleteHandler(scope.row)">删除</el-button>
          </template>
    </el-table-column>
  </el-table>
          <!-- 分页组件 -->
    <Pagination v-bind:child-msg="pageparm" @callFather="callFather"></Pagination>
    </div>
    <el-button type="danger" size="small" style="margin-top: 8px" :disabled="this.multipleSelection.length==0" @click="deleteMany">批量删除</el-button>
    <el-dialog
        title="编辑职位"
        :visible.sync="dialogVisible"
        width="30%">
      <table>
        <tr>
          <td>
            <el-tag>职位编码</el-tag>
          </td>
          <td>
            <el-input v-model="updateJl.code" size="small" style="margin-left: 8px"></el-input>
          </td>
        </tr>
        <tr>
          <td>
            <el-tag>职位名称</el-tag>
          </td>
          <td>
            <el-input v-model="updateJl.positionName" size="small" style="margin-left: 8px"></el-input>
            <!-- <el-input v-model="updateJl.positionName" placeholder="职称等级" style="margin-left: 8px" size="small"></el-input> -->
          </td>
        </tr>
        <tr>
          <td>
            <el-tag>职位级别</el-tag>
          </td>
          <td>
      <el-select v-model="leve" filterable placeholder="请选择">
    <el-option v-for="item in leves" :key="item.code" :label="`${item.code}-${item.typeName}`" :value="`${item.code}-${item.typeName}`"> </el-option>
      </el-select>
            <!-- <el-input v-model="updateJl.leveCode" size="small" style="margin-left: 8px"></el-input> -->
          </td>
        </tr>
        <tr>
          <td>
            <el-tag>职位类别</el-tag>
          </td>
          <td>
             <el-select v-model="category" filterable placeholder="请选择">
        <el-option v-for="item in categorys" :key="item.id"  
            :label="`${item.code}-${item.categoryName}`" :value="`${item.code}-${item.categoryName}`">
       </el-option>
        </el-select>
          </td>
        </tr>
        <tr>
          <td>
            <el-tag>是否启用</el-tag>
          </td>
          <td>
            <el-switch style="margin-left: 8px"
                v-model="updateJl.enabled"
                active-color="#13ce66"
                inactive-color="#ff4949"
                active-text="已启用"
                inactive-text="未启用">
            </el-switch>
          </td>
        </tr>
        <tr>
          <td>
            <el-tag>职位描述</el-tag>
          </td>
          <td>
            <el-input v-model="updateJl.remark" size="small" style="margin-left: 8px"></el-input>
          </td>
        </tr>

      </table>
      <span slot="footer" class="dialog-footer">
    <el-button @click="off()">取 消</el-button>
    <el-button type="primary"  @click="doUpdate()">确 定</el-button>
  </span>
    </el-dialog>

  </div>
</template>

<script>
import Pagination from '../../components/Pagination'
export default {
  name: "JobSeqMana" ,
  mounted() {
    this.initJls(this.pageparm);
      },
    // 注册组件
  components: {
    Pagination
  },
   created() {
    // this.getdata(this.formInline)

  },
  data() {
    return { 
        leves: [],
        categorys: [],
        leve: '',
        category: '',
      //编辑判断(true)  添加判断(false)
      isCompile: false,
      // 分页参数
      pageparm: {
        currentPage: 1,
        pageSize: 10,
        total: 10
      },
      updateJl: {
        code:'',
        positionName: '',
        enabled: '',
        remark: "",
        categoryCode: '',
        leveCode: '',
        categoryName: '',
        leveName: ''
      },
      dialogVisible: false,
      jls: [],
      jl: {
        code: '',
        positionName: ''
      },
      multipleSelection: []
    }
  },
  methods: {
    //查询所有leve
    initdJob(){
        
       this.getRequest('/organization/level/getAll').then(res=>{
          if(res){
            this.leves=res.obj
          }
      })
 
    //查询所有category
      this.getRequest('/organization/type/getAll').then(res=>{
          if(res){
            this.categorys=res.obj
          }
      })
   
    },
     // 搜索事件
    search() {
      this.callFather(this.pageparm)
    },
    // 重置事件
    flush(){
       this.jl.code='',
       this.jl.positionName=''
       this.initJls(this.pageparm)
    },
    //分页
    callFather(dada){
     this.initJls(dada)
    },
    deleteMany() {
      this.$confirm('此操作将永久删除[' + this.multipleSelection.length + ']条职称, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let ids = '?';
        this.multipleSelection.forEach(item=>{
          ids = item.id 
        this.deleteRequest('/organization/sequence/'+ids).then(resp => {
        })})
        this.initJls(this.pageparm)
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    //取消
    off(){
      this.dialogVisible = false;
      this.updateJl.code=''
      this.updateJl.positionName=''
      this.updateJl.enabled=''
      this.updateJl.remark=''
      this.leve=''
      this.category=''
    },
    //确定
    doUpdate() {
       this.updateJl.categoryName= this.category.substring(this.category.indexOf('-')+1,this.category.length);
        this.updateJl.categoryCode= this.category.substring(0,this.category.indexOf('-'));
         this.updateJl.leveName= this.leve.substring(this.leve.indexOf('-')+1,this.leve.length);
         console.log("qqq",this.category)
        this.updateJl.leveCode= this.leve.substring(0,this.leve.indexOf('-'));
      if(this.isCompile){
      this.putRequest('/organization/sequence/',this.updateJl).then(resp=>{
          this.initJls();
          this.dialogVisible = false;
          this.updateJl.code=''
          this.updateJl.positionName=''
          this.updateJl.enabled=false
          this.updateJl.remark=''
          this.leve=''
          this.category=''
      })
      }else{
         if (this.updateJl.code!='' && this.updateJl.positionName!=''
         && this.updateJl.categoryName!='' && this.updateJl.categoryCode!=''
         && this.updateJl.leveName!='' && this.updateJl.leveCode!=''
         ) {
           console.log(this.updateJl)
        this.postRequest('/organization/sequence/',this.updateJl).then(resp=>{
          this.initJls();
          this.dialogVisible = false;
          this.updateJl.code=''
          this.updateJl.positionName=''
          this.updateJl.enabled=false
          this.updateJl.remark=''
          this.leve=''
          this.category=''
        })
      }else {
        this.$message("字段不能为空！");
        this.dialogVisible = true;
      }   
      }
    },

    //编辑
    showEditDialog(data) {
      this.isCompile=true
      data.createDate = '';
      this.leve=data.leveCode+'-'+data.leveName
      this.category=data.categoryCode+'-'+data.categoryName
      Object.assign(this.updateJl, data);
      this.dialogVisible = true;
      this.initdJob();
    },

    //删除
    deleteHandler(data) {
      console.log("1",data)
      this.$confirm('此操作将永久删除[' + data.positionName + ']职称, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.deleteRequest('/organization/sequence/'+data.id).then(resp => {
          if (resp) {
            this.callFather(this.pageparm);
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    //添加
    addJobLevel() { 
        this.isCompile=false
      this.dialogVisible = true;
      this.initdJob();
    },
    //显示
    initJls(dada) {
      if(dada!=undefined){
            this.pageparm.currentPage=dada.currentPage
            this.pageparm.pageSize=dada.pageSize     
      }
      this.getRequest('/organization/sequence/'+ this.pageparm.currentPage+'/'+this.pageparm.pageSize+'?code='+this.jl.code+'&positionName='+this.jl.positionName).then(res=>{
            console.log("111",res)
              this.jls=res.records
              this.pageparm.currentPage=res.current
              this.pageparm.pageSize=res.size
              this.pageparm.total=res.total
    })
    }
  }
}
</script>

<style scoped>

</style>